package cc.holen.htool4j.util;

import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 集合工具类
 *
 * @author holen
 * @date 2022-07-03
 */
public class CollectionUtil {


    /**
     * 判断集合是否不为空
     *
     * @param collection
     * @return
     */
    public static boolean isNotEmpty(Collection<?> collection) {
        return !CollectionUtils.isEmpty(collection);
    }

    /**
     * 判断map不为空
     *
     * @param map
     * @return
     */
    public static boolean isNotEmpty(Map<?, ?> map) {
        return !CollectionUtils.isEmpty(map);
    }

    /**
     * 获取集合的size,为空或者null的话都为0
     *
     * @param collection
     * @return
     */
    public static int getSize(Collection<?> collection) {
        return isNotEmpty(collection) ? collection.size() : 0;
    }

    /**
     * 获取集合中指定书目的子集
     *
     * @param list
     * @param length
     * @return
     */
    public static List<?> tryGet(List<?> list, int length) {
        if (getSize(list) <= length) {
            return list;
        }
        return list.subList(0, length);
    }

}
